using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
namespace MicroMsg.Scene
{
	public class NetSceneGetPackageList : NetSceneBaseEx<GetPackageListRequest, GetPackageListResponse, GetPackageListRequest.Builder>
	{
		private const string TAG = "NetSceneGetPackageList";
		private const int STATUS_COMPLETE = 1;
		private const int STATUS_GETTING = 0;
		public const string TimeStampKey = "dbLastGetEmojiArtTimeStamp";
		public const string TimeEggStampKey = "dbLastGetEggTimeStamp";
		private List<EmojiArtPackageInfo> mEmojiArtPackageList;
		private int getListTimes;
		private static int mStatus = 1;
		private List<Package> listLocal;
		private AllLanguageMasterMap allMasterMap;
		public bool doScene(int type)
		{
			if (NetSceneGetPackageList.mStatus != 1)
			{
				Log.smethod_4("NetSceneGetPackageList", "get package list is not complete");
				return false;
			}
			return this.doSceneEx(type);
		}
		private bool doSceneEx(int type)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/getpackagelist";
			this.mSessionPack.mConnectMode = 2;
			if (type == 2)
			{
				NetSceneGetPackageList.mStatus = 0;
				List<EmojiArtGroup> list = StorageMgr.emojiArtGroup.getList();
				if (list == null)
				{
					list = new List<EmojiArtGroup>();
				}
				using (List<EmojiArtGroup>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						EmojiArtGroup current = enumerator.get_Current();
						Package.Builder builder = Package.CreateBuilder();
						builder.Id = current.nPackID;
						builder.Version = current.nPackVersion;
						if (current.strPackName_zh_cn == null)
						{
							current.strPackName_zh_cn = "";
						}
						builder.PackName = current.strPackName_zh_cn;
						if (current.strName == null)
						{
							current.strName = "";
						}
						builder.Name = current.strName;
						Package package = builder.Build();
						this.mBuilder.ListList.Add(package);
					}
				}
				this.mBuilder.Count = (uint)this.mBuilder.ListList.get_Count();
				Log.smethod_5("NetSceneGetPackageList", "mBuilder.Count = " + this.mBuilder.Count);
				this.mBuilder.Type = 2u;
				base.endBuilder();
				this.getListTimes++;
				return true;
			}
			if (type == 4)
			{
				if (this.listLocal == null)
				{
					this.mBuilder.Count = 0u;
				}
				else
				{
					this.mBuilder.Count = (uint)this.listLocal.get_Count();
					using (List<Package>.Enumerator enumerator2 = this.listLocal.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Package current2 = enumerator2.get_Current();
							this.mBuilder.ListList.Add(current2);
						}
					}
				}
				NetSceneGetPackageList.mStatus = 0;
				this.mBuilder.Type = (uint)type;
				base.endBuilder();
				return true;
			}
			this.mBuilder.Count = 0u;
			Log.smethod_5("NetSceneGetPackageList", "mBuilder.Count = " + this.mBuilder.Count);
			NetSceneGetPackageList.mStatus = 0;
			this.mBuilder.Type = (uint)type;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(GetPackageListRequest request, GetPackageListResponse response)
		{
			Log.smethod_1("NetSceneGetPackageList", "Get Package List failed system error!");
			this.doSenceEnd(request, response);
			EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_ERR, null, null);
		}
		protected override void onSuccess(GetPackageListRequest request, GetPackageListResponse response)
		{
			if (response == null || response.BaseResponse.Ret != 0)
			{
				Log.smethod_1("NetSceneGetPackageList", string.Concat(new object[]
				{
					"Get Package List response Ret = ",
					response.BaseResponse.Ret.ToString(),
					" type = ",
					request.Type
				}));
				this.doSenceEnd(request, response);
				EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_ERR, null, null);
				return;
			}
			Log.smethod_4("NetSceneGetPackageList", "get package list response correct");
			if (response.Type != 2u)
			{
				if (response.Type == 4u)
				{
					if (response.ListList.get_Count() <= 0)
					{
						Log.smethod_1("NetSceneGetPackageList", "get MM_PACKAGE_ALBUM response.ListList.Count == 0");
						EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_ERR, null, null);
					}
					string currentLanguage = AlbumBackgroundMgr.getCurrentLanguage();
					using (IEnumerator<Package> enumerator = response.ListList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Package current = enumerator.get_Current();
							if (this.listLocal == null)
							{
								this.listLocal = new List<Package>();
							}
							this.listLocal.Add(current);
							if (current.Thumb != null)
							{
								byte[] array = current.Thumb.Buffer.ToByteArray();
								string @string = Encoding.get_UTF8().GetString(array, 0, array.Length);
								if (!string.IsNullOrEmpty(@string))
								{
									if (this.allMasterMap == null)
									{
										this.allMasterMap = new AllLanguageMasterMap();
									}
									MasterAlbumList masterAlbumList = AlbumBackgroundMgr.parasXmlToMasterAlbum(@string);
									masterAlbumList.lastUpdateTime = Util.getNowMilliseconds();
									if (masterAlbumList != null)
									{
										this.allMasterMap.allLanguageMasterMap.Add(masterAlbumList.language, masterAlbumList);
										masterAlbumList.language == currentLanguage;
										if (response.ContinueFlag == 1u)
										{
											this.doSceneEx(4);
											return;
										}
									}
									AlbumBackgroundMgr.saveMasterAlbum(this.allMasterMap);
									if (!this.allMasterMap.allLanguageMasterMap.ContainsKey(currentLanguage))
									{
										if (this.allMasterMap.allLanguageMasterMap.ContainsKey("en"))
										{
											EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.allMasterMap.allLanguageMasterMap.get_Item("en"), null);
										}
										else
										{
											EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_ERR, null, null);
										}
										this.doSenceEnd(request, response);
										return;
									}
									if (this.allMasterMap.allLanguageMasterMap.get_Item(currentLanguage) == null)
									{
										EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_ERR, null, null);
									}
									else
									{
										EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.allMasterMap.allLanguageMasterMap.get_Item(currentLanguage), null);
									}
									this.doSenceEnd(request, response);
									return;
								}
							}
						}
						return;
					}
				}
				if (response.Type == 1u)
				{
					PacageDataList pacageDataList = new PacageDataList();
					using (IEnumerator<Package> enumerator2 = response.ListList.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Package current2 = enumerator2.get_Current();
							if (current2.Thumb != null)
							{
								PacageData pacageData = new PacageData();
								pacageData.nId = current2.Id;
								pacageData.Name = current2.Name;
								pacageData.nSize = (int)current2.Size;
								pacageData.nVersion = current2.Version;
								pacageData.strPackName = current2.PackName;
								byte[] data = current2.Thumb.Buffer.ToByteArray();
								string text = StorageIO.getImagePath() + "/" + MD5Core.GetHashString(Util.getNowMilliseconds() + pacageData.nId) + ".jpg";
								StorageIO.writeToFile(text, data, false);
								pacageData.thumFileName = text;
								pacageData.nType = (int)response.Type;
								pacageDataList.dataList.Add(pacageData);
							}
						}
					}
					pacageDataList.lastUpdateTime = Util.getNowMilliseconds();
					PackageDataMgr.savePacageDataList(pacageDataList);
					EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, pacageDataList, null);
					this.doSenceEnd(request, response);
				}
				else
				{
					if (response.Type == 6u)
					{
						new PacageDataList();
						using (IEnumerator<Package> enumerator3 = response.ListList.GetEnumerator())
						{
							while (enumerator3.MoveNext())
							{
								Package current3 = enumerator3.get_Current();
								if (current3.Thumb != null)
								{
									string strXml = Util.ByteArrayToString(current3.Thumb.Buffer.ToByteArray());
									EggItemList eggItemList = EggProfileMgr.parasEggProfileXml(strXml);
									if (eggItemList != null)
									{
										EggProfileMgr.saveEggProfile(eggItemList);
									}
								}
							}
						}
						this.doSenceEnd(request, response);
						return;
					}
					return;
				}
				return;
			}
			if (this.mEmojiArtPackageList == null)
			{
				this.mEmojiArtPackageList = new List<EmojiArtPackageInfo>();
			}
			this.mEmojiArtPackageList.Clear();
			using (IEnumerator<Package> enumerator4 = response.ListList.GetEnumerator())
			{
				while (enumerator4.MoveNext())
				{
					Package current4 = enumerator4.get_Current();
					if (current4.Thumb != null)
					{
						byte[] array2 = current4.Thumb.Buffer.ToByteArray();
						string text2 = Encoding.get_UTF8().GetString(array2, 0, array2.Length);
						if (!string.IsNullOrEmpty(text2))
						{
							if (!text2.StartsWith("<"))
							{
								Log.smethod_5("NetSceneGetPackageList", "strXml need cut");
								int num = text2.IndexOf("<", 0);
								text2 = text2.Substring(num);
							}
							if (!string.IsNullOrEmpty(text2))
							{
								EmojiArtPackageInfo emojiArtPackageInfo = this.XmlToEmojiArtPackageInfo(text2);
								if (emojiArtPackageInfo != null)
								{
									emojiArtPackageInfo.emojiArtGroup.strName = current4.Name;
									emojiArtPackageInfo.emojiArtGroup.nPackVersion = current4.Version;
									uint arg_124_0 = current4.Size;
									this.mEmojiArtPackageList.Add(emojiArtPackageInfo);
								}
							}
						}
					}
				}
			}
			if (this.mEmojiArtPackageList.get_Count() > 0)
			{
				Log.smethod_5("NetSceneGetPackageList", "EmojiArtPackageList count = " + this.mEmojiArtPackageList.get_Count());
				List<EmojiArtGroup> list = new List<EmojiArtGroup>();
				using (List<EmojiArtPackageInfo>.Enumerator enumerator5 = this.mEmojiArtPackageList.GetEnumerator())
				{
					while (enumerator5.MoveNext())
					{
						EmojiArtPackageInfo current5 = enumerator5.get_Current();
						list.Add(current5.emojiArtGroup);
						EmojiArtGroup emojiArtGroup = StorageMgr.emojiArtGroup.method_1(current5.emojiArtGroup.nPackID);
						if (emojiArtGroup != null && emojiArtGroup.nPackVersion != current5.emojiArtGroup.nPackVersion && emojiArtGroup.nPackID != 1)
						{
							Log.smethod_4("NetSceneGetPackageList", "emojiArt package clean all item packid = " + emojiArtGroup.nPackID);
							StorageMgr.emojiArt.delPack(emojiArtGroup.nPackID);
						}
						StorageMgr.emojiArt.updateList(current5.mEmojiArtItemList);
					}
				}
				if (list.get_Count() > 0)
				{
					StorageMgr.emojiArtGroup.updateList(list);
				}
			}
			else
			{
				Log.smethod_4("NetSceneGetPackageList", "get EmojiArt Package List count = 0");
			}
			if (response.ContinueFlag == 1u && this.getListTimes < 15)
			{
				Log.smethod_4("NetSceneGetPackageList", "continue get package list");
				this.doSceneEx(2);
				return;
			}
			Log.smethod_4("NetSceneGetPackageList", "get package list success");
			List<EmojiArtGroup> list2 = StorageMgr.emojiArtGroup.getList();
			if (list2 != null && list2.get_Count() > 2)
			{
				this.updateLastGetEmojiArtTime();
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.mEmojiArtPackageList.get_Count(), null);
			this.doSenceEnd(request, response);
		}
		private void doSenceEnd(GetPackageListRequest request, GetPackageListResponse response)
		{
			Log.smethod_4("NetSceneGetPackageList", "doSenceEnd");
			if (request.Type == 6u)
			{
				ConfigMgr.setDouble("dbLastGetEggTimeStamp", Util.getNowMilliseconds());
			}
			if (request.Type == 2u)
			{
				this.updateLastGetEmojiArtTime();
			}
			NetSceneGetPackageList.mStatus = 1;
		}
		private void updateLastGetEmojiArtTime()
		{
			ConfigMgr.setDouble("dbLastGetEmojiArtTimeStamp", Util.getNowMilliseconds());
		}
		private EmojiArtPackageInfo XmlToEmojiArtPackageInfo(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				Log.smethod_5("NetSceneGetPackageList", "Thumb is null");
				return null;
			}
			EmojiArtPackageInfo emojiArtPackageInfo = new EmojiArtPackageInfo();
			emojiArtPackageInfo.emojiArtGroup = new EmojiArtGroup();
			if (emojiArtPackageInfo.mEmojiArtItemList == null)
			{
				emojiArtPackageInfo.mEmojiArtItemList = new List<EmojiArt>();
			}
			emojiArtPackageInfo.mEmojiArtItemList.Clear();
			EmojiArtPackageInfo result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("EmojiArtCatalog");
				if (xElement != null)
				{
					string value = xElement.Attribute("name").get_Value();
					this.getName(value, out emojiArtPackageInfo.emojiArtGroup.strPackName_zh_cn, out emojiArtPackageInfo.emojiArtGroup.strPackName_en_us, out emojiArtPackageInfo.emojiArtGroup.strPackName_zh_tw);
					string value2 = xElement.Attribute("id").get_Value();
					int nPackID = int.Parse(value2);
					emojiArtPackageInfo.emojiArtGroup.nPackID = nPackID;
					string arg_E9_0 = xElement.Attribute("version").get_Value();
					XElement xElement2 = xElement.Element("Icon");
					if (xElement2 != null)
					{
						emojiArtPackageInfo.emojiArtGroup.strIcon = NetSceneGetPackageList.decodeBase64("UTF-8", xElement2.get_Value());
					}
					using (IEnumerator<XElement> enumerator = xElement.Elements().GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							XElement current = enumerator.get_Current();
							if (current.get_HasAttributes() && current.Attribute("name") != null)
							{
								string value3 = current.Attribute("name").get_Value();
								EmojiArt emojiArt = new EmojiArt();
								this.getName(value3, out emojiArt.strName_zh_cn, out emojiArt.strName_en_us, out emojiArt.strName_zh_tw);
								string value4 = current.get_Value();
								emojiArt.strEmojiArt = NetSceneGetPackageList.decodeBase64("UTF-8", value4);
								emojiArt.strMD5 = MD5Core.GetHashString(emojiArt.strEmojiArt + nPackID.ToString());
								emojiArt.nPackID = nPackID;
								emojiArtPackageInfo.mEmojiArtItemList.Add(emojiArt);
							}
						}
					}
				}
				result = emojiArtPackageInfo;
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneGetPackageList", ex.ToString());
				result = null;
			}
			return result;
		}
		public bool getName(string attr, out string cnName, out string enName, out string twName)
		{
			if (string.IsNullOrEmpty(attr))
			{
				cnName = null;
				enName = null;
				twName = null;
				return false;
			}
			int num = attr.IndexOf("zh_CN:", 0);
			string text = attr.Substring(num + 6);
			int num2 = text.IndexOf(";zh_TW:", 0);
			cnName = text.Substring(0, num2);
			text = text.Substring(num2 + 7);
			int num3 = text.IndexOf(";en:", 0);
			twName = text.Substring(0, num3);
			text = text.Substring(num3 + 4);
			int num4 = text.IndexOf(";", 0);
			enName = text.Substring(0, num4);
			return true;
		}
		public static string decodeBase64(string code_type, string code)
		{
			string result = "";
			byte[] array = Convert.FromBase64String(code);
			try
			{
				result = Encoding.GetEncoding(code_type).GetString(array, 0, array.Length);
			}
			catch
			{
				result = code;
			}
			return result;
		}
	}
}
